<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>vue</title>
  <script src="../js/vue.js"></script>
  <script src="../js/vue-router.js"></script>
</head>
<body>
  <div id="app">
    <!-- 4.roter使用 跳转链接 加载组件的区域 -->
    <router-link to="/a/1/name/zhangsan">点击跳转A</router-link>
    <router-link to="/b">点击跳转B</router-link>
    <div>
      <!-- 加载组件的区域 -->
      <router-view></router-view>
    </div>
  </div>
  <script>
    // 1.组件定义
    let ComponentA = {
      data(){
        return {
          id: null,
          name: ''
        }
      },
      template: `
        <div>组件A内容
          <div>id:{{id}}</div>
          <div>name:{{name}}</div>
        </div>
      `,
      beforeRouteEnter(to,from,next){
        console.log('路由进入之前');
        next();
      },
      // 组件内部的导航守卫
      beforeRouteUpdate(to,from,next) {
        console.log('组件路由更新');
        this.id = to.params.id
        this.name = to.params.name
        next();
      },
      beforeRouteLeave(to,from,next){
        console.log('路由跳转，离开组件');
        next();
      },
      // watch: {
      //   // msg(newValue, oldValue) {

      //   // },
      //   $route(to,from) {
      //     // console.log(to,from);
      //     this.id = to.params.id
      //     this.name = to.params.name
      //   }
      // },
      created(){
        // console.log(this.$route.params);
        this.id = this.$route.params.id
        this.name = this.$route.params.name
      }
    }
    let ComponentB = {
      template: `
        <div>组件B内容</div>
      `
    }
    // 2.路由的定义
    let router = new VueRouter({
      routes:[
        // { path: '/', redirect: { name: 'aRouter'} },
        { path: '/a/:id/name/:name', name: 'aRouter', component: ComponentA, alias: '/A' },
        { path: '/b', name: 'bRouter', component: ComponentB },
      ]
    })
    let vm = new Vue({
      el: '#app',
      // 3.组件内注册路由
      router: router,
      data: {

      }
    })
  </script>
</body>
</html>